#include "ChatDialog/chatdialog.h"
#include "ChatDialog/ui_chatdialog.h"

#include "ChatDialog/chatuserwid.h"
#include "ChatDialog/loadingdlg.h"

#include <QAction>
#include <QListWidgetItem>

ChatDialog::~ChatDialog(){
    delete ui;
}

ChatDialog::ChatDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::ChatDialog)
    , _mode(ChatUIMode::ChatMode)
    , _state(ChatUIMode::ChatMode)
    , _b_loading(false)
{
    ui->setupUi(this);

    // ChatDialog 界面的优化 //

    // 1. ui->side_bar 部分的设置
    // 用 QImage 和 QPixmap 设置 QLabel上显示图片
    QImage img(":/res/person.jpg");
    ui->side_head_lb->setPixmap(QPixmap::fromImage(img)); // tcp 长连接聊天，个人头像的设置
    ui->side_head_lb->setScaledContents(true); // 让图片填满整个 QLabel
    // 好友列表
    ui->side_contact_list_lb->SetState("normal", "hover", "press", "", "", "");
    ui->side_contact_list_lb->setCursor(Qt::PointingHandCursor);
    connect(ui->side_contact_list_lb, &ClickedLabel::clicked, this, &ChatDialog::slot_side_contact_list_lb);
    // 聊天列表
    ui->side_chat_lb->SetState("normal", "hover", "press", "", "", "");
    ui->side_chat_lb->setCursor(Qt::PointingHandCursor);
    connect(ui->side_chat_lb, &ClickedLabel::clicked, this, &ChatDialog::slot_side_chat_lb);


    // 2. ui->chat_user_wid 部分的设置
    // ui->add_btn 提升为 ClickedBtn 了，就可以调用 class::ClickedBtn 的所有成员函数了
    ui->add_btn->SetState("normal", "hover", "press");

    // 搜索框的设置
    QAction* searchAction = new QAction(ui->search_edit);
    searchAction->setIcon(QIcon(":/res/search.png"));
    ui->search_edit->addAction(searchAction, QLineEdit::LeadingPosition); // 搜索图标放在前面的位置上（下面的清除图标放在后面的位置上）
    ui->search_edit->setPlaceholderText(QStringLiteral("搜索"));
    // 创建一个清除动作并设置图标
    QAction* clearAction = new QAction(ui->search_edit);
    clearAction->setIcon(QIcon(":/res/close_transparent.png"));
    ui->search_edit->addAction(clearAction, QLineEdit::TrailingPosition); // 初始时不显示清除图标
    // 当需要显示清除图标时，更改为实际的清除图标
    connect(ui->search_edit, &QLineEdit::textChanged, [clearAction](const QString &text) {
        if (!text.isEmpty())
            clearAction->setIcon(QIcon(":/res/close_search.png"));
        else
            clearAction->setIcon(QIcon(":/res/close_transparent.png")); // 文本为空时，切换回透明图标
    });
    // 连接清除动作的触发信号到槽函数，用于清除文本
    connect(clearAction, &QAction::triggered, [this, clearAction]() {
        ui->search_edit->clear();
        clearAction->setIcon(QIcon(":/res/close_transparent.png")); // 清除文本后，切换回透明图标
        ui->search_edit->clearFocus();
        // 清除按钮被按下则不显示搜索框
        ShowSearch(false);
    });
    ShowSearch(false);

    ui->search_edit->SetMaxLength(15); // 设置输入框的最大长度

    connect(ui->chat_user_list, &ChatUserList::sig_loading_chat_user, this, &ChatDialog::slot_loading_chat_user); // 加载更多聊天信息的信号，连接，加载更多聊天信息的槽函数
    addChatUserList(); // debug: 添加聊天信息

    // 3. ui->chat_data_wid 部分的设置
    // ui->chat_name_lb 聊天人昵称的设置
    ui->chat_name_lb->setText("hello world");
    // ui->chat_edit 输入聊天信息的 QTextEdit 取消滑动条的效果（直接在设计师界面中设置verticalScrollBarPolicy为ScrollBarAlwaysOff即可）
    // 表情图标的设置
    ui->emm_lb->SetState("normal", "hover", "press", "", "", "");
    ui->emm_lb->setCursor(Qt::PointingHandCursor);
    connect(ui->emm_lb, &ClickedLabel::clicked, this, &ChatDialog::slot_emm_lb);
    // 文件图标的设置
    ui->file_lb->SetState("normal", "hover", "press", "", "", "");
    ui->file_lb->setCursor(Qt::PointingHandCursor);
    connect(ui->file_lb, &ClickedLabel::clicked, this, &ChatDialog::slot_file_lb);

}

// 加载更多聊天信息的槽函数
void ChatDialog::slot_loading_chat_user(){
    if(_b_loading == true) return; // 多次快速点击可能造成数据竞争

    _b_loading = true;
    LoadingDlg* loadingDialog = new LoadingDlg(this);
    loadingDialog->setModal(true);
    loadingDialog->show();
    qDebug() << "add new data to list......";
    addChatUserList(); // 加载跟多聊天信息
    loadingDialog->deleteLater(); // 加载完后关闭对话框

    _b_loading = false;
}
// 创建QListWidgetItem，并设置自定义的widget
void ChatDialog::addChatUserList(){
    // 下面的数据实际是通过后端传递过来的
    std::vector<QString>  strs = {"hello world !",
                                 "nice to meet u",
                                 "New year，new life",
                                 "You have to love yourself",
                                 "My love is written in the wind ever since the whole world is you"};

    std::vector<QString> heads = {
        ":/res/head_1.jpg",
        ":/res/head_2.jpg",
        ":/res/head_3.jpg",
        ":/res/head_4.jpg",
        ":/res/head_5.jpg"};

    std::vector<QString> names = {
        "llfc",
        "zack",
        "golang",
        "cpp",
        "java",
        "nodejs",
        "python",
        "rust"};


    for(int i = 0; i < 13; i++){
        int randomValue = QRandomGenerator::global()->bounded(100); // 生成0到99之间的随机整数
        int str_i = randomValue % strs.size();
        int head_i = randomValue % heads.size();
        int name_i = randomValue % names.size();

        auto* chat_user_wid = new ChatUserWid();
        chat_user_wid->SetInfo(names[name_i], heads[head_i], strs[str_i]);
        QListWidgetItem* item = new QListWidgetItem;
        // qDebug() << "chat_user_wid sizeHint is " << chat_user_wid->sizeHint();
        item->setSizeHint(chat_user_wid->sizeHint());
        ui->chat_user_list->addItem(item);
        ui->chat_user_list->setItemWidget(item, chat_user_wid);
    }
}

// ui->chat_user_wid 显示哪一个 list 的设置
void ChatDialog::ShowSearch(bool bsearch){
    if(bsearch){
        ui->chat_user_list->hide();
        ui->con_user_list->hide();
        ui->search_list->show();
        _mode = ChatUIMode::SearchMode;
    }else if(_state == ChatUIMode::ChatMode){
        ui->chat_user_list->show();
        ui->con_user_list->hide();
        ui->search_list->hide();
        _mode = ChatUIMode::ChatMode;
    }else if(_state == ChatUIMode::ContactMode){
        ui->chat_user_list->hide();
        ui->search_list->hide();
        ui->con_user_list->show();
        _mode = ChatUIMode::ContactMode;
    }
}

// ChatDialog 界面 ClickedLabel 被点击触发的槽函数
void ChatDialog::slot_emm_lb(){
    qDebug() << "ui->emm_lb clicked......";
}
void ChatDialog::slot_file_lb(){
    qDebug() << "ui->file_lb clicked......";
}
void ChatDialog::slot_side_contact_list_lb(){
    qDebug() << "ui->side_contact_list_lb clicked......";
}
void ChatDialog::slot_side_chat_lb(){
    qDebug() << "ui->side_chat_lb clicked......";
}

